package com.ftwj.demo.permission.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftwj.demo.permission.dao.RoleResourceDao;
import com.ftwj.demo.permission.entity.RoleResourceBean;
import com.ftwj.demo.permission.service.RoleResourceService;
import com.ftwj.demo.global.shiro.ShiroUtil;

/**
*
* <p>Title: RoleServiceImpl</p>  
* <p>Description: 角色管理业务实现类</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author WangSLi
* @date 2020年4月4日 下午5:58:20
* @version V1.0
*/
@Service
public class RoleRosourceServiceImpl extends ServiceImpl<RoleResourceDao, RoleResourceBean> implements RoleResourceService {
	
	@Autowired
	private RoleResourceDao dao;

	@Override
	public boolean saveRoleResource(Integer roleId, Integer[] resourceIds) {
		//先删该角色权限
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("role_id", roleId);
		dao.deleteByMap(map);
		//新增权限
		boolean flag = true;
		RoleResourceBean roleResource = new RoleResourceBean();
		for (int rid : resourceIds) {
			roleResource.setRoleId(roleId);
			roleResource.setResourceId(rid);
			roleResource.setCreateUser(ShiroUtil.getUserId());
			roleResource.setCreateTime(new Date());
			roleResource.setState(1);
			int i = dao.insert(roleResource);
			if (i != 1) flag = false;
		}
		return flag;
	}

}
